package com.zhijian.medical.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.zhijian.medical.dao.local.DeviceMaintenanceRecordMapper;
import com.zhijian.medical.entity.po.Device;
import com.zhijian.medical.entity.po.DeviceMaintenanceRecord;
import com.zhijian.medical.entity.vo.request.DeviceMaintenanceRecordSearchVo;
import com.zhijian.medical.entity.vo.response.DeviceMaintenanceRecordVo;
import com.zhijian.medical.service.DeviceMaintenanceRecordService;
import com.zhijian.medical.service.DeviceService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 维护记录业务处理层
 *
 * @author HCG
 * @date 2022-01-01 15:39:46
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceMaintenanceRecordServiceImpl implements DeviceMaintenanceRecordService {

    @Autowired
    private DeviceMaintenanceRecordMapper mapper;

    @Autowired
    private DeviceService deviceService;

    @Override
    public DeviceMaintenanceRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceMaintenanceRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceMaintenanceRecord deviceMaintenanceRecord) {
        Device device = deviceService.selectById(deviceMaintenanceRecord.getDeviceId());
        AssertUtil.notNull(device, "此设备不存在或者已被删除");
        Date now = new Date();
        //保养内容
        deviceMaintenanceRecord.setSummary(device.getMaintenanceContent());
        deviceMaintenanceRecord.setCreateTime(now);
        deviceMaintenanceRecord.setUpdateTime(now);
        return mapper.insert(deviceMaintenanceRecord) == 1 ? deviceMaintenanceRecord.getId() : 0L;
    }

    @Override
    public boolean update(DeviceMaintenanceRecord deviceMaintenanceRecord) {
        Date now = new Date();
        deviceMaintenanceRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceMaintenanceRecord) != 0;
    }

    @Override
    public Integer count(DeviceMaintenanceRecordSearchVo deviceMaintenanceRecordSearchVo) {
        return mapper.count(deviceMaintenanceRecordSearchVo);
    }

    @Override
    public List<DeviceMaintenanceRecordVo> selectList(DeviceMaintenanceRecordSearchVo deviceMaintenanceRecordSearchVo) {
        return mapper.selectList(deviceMaintenanceRecordSearchVo);
    }

    @Override
    public Page<DeviceMaintenanceRecordVo> selectPage(DeviceMaintenanceRecordSearchVo deviceMaintenanceRecordSearchVo) {
        //先直接查总数
        int count = count(deviceMaintenanceRecordSearchVo);
        //分页
        Page<DeviceMaintenanceRecordVo> page = new Page<>(true, count, deviceMaintenanceRecordSearchVo.getPageNum(), deviceMaintenanceRecordSearchVo.getPageSize());
        deviceMaintenanceRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceMaintenanceRecordVo> responseVos = this.selectList(deviceMaintenanceRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public void export(Long deviceId, HttpServletResponse response) {
        List<DeviceMaintenanceRecordVo> deviceMaintenanceRecordVos = this.selectList(DeviceMaintenanceRecordSearchVo.builder()
                .deviceId(deviceId)
                .build());
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("date", "维护日期")
                .addHeaderAlias("summary", "维护简介")
                .addHeaderAlias("maintenancePerson", "维护人");
        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(2, "维护记录");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        writer.write(deviceMaintenanceRecordVos, true);

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", String.format("attachment;filename=%s.xlsx", "维护记录"));
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
            IoUtil.close(out);
        }
    }

}